<?php

namespace App\Http\Controllers;
use App\Models\Topic;
use App\Models\Post;
use App\Models\Message;
use App\Models\User;
use App\Models\SubtopicModerator;
use App\Models\SubtopicSubscriber;
use Auth;
use Carbon\Carbon;
use App\Http\Requests\NewMessageRequest;
use App\Http\Requests\NewSubtopicRequest;
use App\Http\Requests\NewSubtopicModeratorRequest;

use Illuminate\Http\Request;

class WroteitController extends Controller
{
    // Views call and stuff   

    public function main(){
		if(Auth::check()){
			$subbed = SubtopicSubscriber::select('subtopic')->where('subscriber_id',Auth::user()->id);
        	$posts = Post::orderby('updated_at','desc')->whereIn('subwroteit',$subbed)->get();
		}
		else{
			$posts = Post::orderby('updated_at','desc')->get();
		}
			$posts = Post::orderby('updated_at','desc')->get();
        return view('wroteit.main')->with(['posts' => $posts]);
    }

    public function show_topic(string $sub){
		$topic = Topic::where('title',$sub)->first();
		$posts = Post::where('subwroteit',$sub)->get();
        $not_subbed = false;
        $not_moderator = false;
        if(Auth::check()){
            $isSubbed = SubtopicSubscriber::where('subscriber_id', '=', Auth::user()->id)->where('subtopic',$sub)->first();

            if(is_null($isSubbed)){
                $not_subbed = true;
            }
        }

        if(Auth::check()){
            $isModerator = SubtopicModerator::where('moderator_id','=',Auth::user()->id)->where('subtopic',$sub)->first();

            if(is_null($isModerator)){
                $not_moderator = true;
            }
        }

        return view('wroteit.show_topic')->with(['topic' => $topic])->with(['posts' => $posts])->with(['not_subbed' => $not_subbed])->with(['not_moderator' => $not_moderator]);
    }

    public function messages_main(){
        return view('wroteit.messages');
    }

    public function messages_receive(){
        $messages = Message::orderby('created_at','desc')->paginate(10);
        return view('wroteit.receive_messages')->with(['messages' => $messages]);
    }

    public function show_message(Message $mes){ // for received messages
        if(Auth::check()){
            if(Auth::user()->id == $mes->to_id){    // if the user can see this mail it goes with no problem
                return view('wroteit.show_message')->with(['message' => $mes]);
            }else{          // if it is not supposed to see it, he's redirected to the main page
                $topics = Topic::orderby('updated_at','desc')->paginate(6);
                return redirect()->route('main')->with(['topics' => $topics]);
            }
        }else{
            $topics = Topic::orderby('updated_at','desc')->paginate(6);
            return redirect()->route('main')->with(['topics' => $topics]);
        }        
    }

    public function messages_send(){
        $messages = Message::orderby('created_at','desc')->paginate(10);
        return view('wroteit.send_messages')->with(['messages' => $messages]);
    }

    public function show_message2(Message $mes){ // for send messages
        if(Auth::check()){
            if(Auth::user()->id == $mes->from_id){    // if the user can see this mail it goes with no problem
                return view('wroteit.show_message2')->with(['message' => $mes]);
            }else{          // if it is not supposed to see it, he's redirected to the main page
                $topics = Topic::orderby('updated_at','desc')->paginate(6);
                return redirect()->route('main')->with(['topics' => $topics]);
            }
        }else{
            $topics = Topic::orderby('updated_at','desc')->paginate(6);
            return redirect()->route('main')->with(['topics' => $topics]);
        }      
    }

    public function new_message(){
        $users = User::where('id', '!=', auth()->id())->get();
        return view('wroteit.send_message_to')->with(['users' => $users]);
    }

    public function store_new_message(NewMessageRequest $request){
        $mes = Message::create($request->only('title','body','from_id','to_id','created_at','updated_at'));
        return redirect()->route('show_message2',['mes'=>$mes->id]);
    }

    public function new_subtopic(){
        $subtopics = Topic::orderby('created_at','desc');
        return view('wroteit.new_subtopic')->with(['subtopics' => $subtopics]);
    }

    public function store_new_subtopic(NewSubtopicRequest $request){
        if(Auth::check()){
            Topic::create($request->only('title','description','posts_number','subscriptions','created_at','updated_at'));
            $subtopic_name = $request->input('title');
            $user_id = Auth::user()->id;
            $this->store_relation_subtopicModerator($subtopic_name, $user_id);
            $this->store_relation_subtopicSubscriber($subtopic_name, $user_id);
            return redirect()->route('main');
        }        
    }

    public function store_relation_subtopicModerator($subtopic_name, $user_id){
        $rel = array('subtopic' => $subtopic_name, 'moderator_id' => $user_id);
        SubtopicModerator::create($rel);
    }

    public function store_relation_subtopicSubscriber($subtopic_name, $user_id){
        $rel = array('subtopic' => $subtopic_name, 'subscriber_id' => $user_id);
        SubtopicSubscriber::create($rel);
    }

    public function add_moderator(string $sub){
        $mods = SubtopicModerator::select('moderator_id')->where('subtopic','=',$sub)->get();
        $not_mods = User::whereNotIn('id', $mods)->get();
        return view('wroteit.new_mod')->with(['subtopic'=>$sub])->with(['not_mods'=>$not_mods]);
    }

    public function store_moderator(NewSubtopicModeratorRequest $request){
        SubtopicModerator::create($request->only('subtopic','moderator_id'));
        $subtopic_name = $request->input('subtopic');
        return redirect()->route('main');
    }

    // Static methods for injection

    public static function getSender(Message $mes){       
        return User::find($mes->from_id);
    }

    public static function getReceiver(Message $mes){
        return User::find($mes->to_id);
    }  

    public static function getCurrentTimeStamp(){
        return Carbon::now();       // the Carbon model is implemented with Laravel. It gives a ton of stuff to manage timestamps=>useful
    }
}
